//
//  PhotoBrowserCell.swift
//  Super_WeiBo
//
//  Created by SuperMac on 16/5/6.
//  Copyright © 2016年 lc. All rights reserved.
//

import UIKit
import SDWebImage

protocol PhotoBrowserCellDelegate: NSObjectProtocol
{
    func photoBrowserCellDidClose(cell: PhotoBrowserCell)
}

class PhotoBrowserCell: UICollectionViewCell
{
    
    weak var photoBrowserCellDelegate: PhotoBrowserCellDelegate?
    
    var imageURL: NSURL?
    {
        didSet
        {
            // 重置属性
            reset()
            
            // 设置图片
            iconView.sd_setImageWithURL(imageURL) { (image, _, _, _) in
                // 调整图片显示 位置
                self.setImageViewPosition()
            }
        }
    }
    /**
     调整图片显示位置
     */
    private func setImageViewPosition()
    {
        // 拿到按照宽高比计算之后的图片大小
        let size = self.displaySize(iconView.image!)
        // 判断图片高度是否超过屏幕高度
        if size.height < UIScreen.mainScreen().bounds.height
        {// 短图
            // 设置图片大小
            iconView.frame = CGRect(origin: CGPointZero, size: size)
            // 设置居中显示
            let spen = (UIScreen.mainScreen().bounds.height - size.height) * 0.5
            scrollview.contentInset = UIEdgeInsets(top: spen, left: 0, bottom: spen, right: 0)
            
        }
        else
        {// 长图
            // 设置图片大小
            iconView.frame = CGRect(origin: CGPointZero, size: size)
            // 设置滚动视图滚动范围
            scrollview.contentSize = size
        }
    }
    /**
     重置scrollView和imagView属性
     */
    private func reset()
    {
        // 重置scrollView
        scrollview.contentInset  = UIEdgeInsetsZero
        scrollview.contentOffset = CGPointZero
        scrollview.contentSize = CGSizeZero
        // 重置图片缩放
        iconView.transform = CGAffineTransformIdentity
    }
    
    /**
     计算图片缩放后的尺寸
     */
    private func displaySize(image: UIImage) -> CGSize
    {
        // 计算高宽比
        let scale = image.size.height / image.size.width
        let width = UIScreen.mainScreen().bounds.width
        // 宽度已知，计算高度
        let height = width * scale
        return CGSize(width: width, height: height)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 初始化UI
        setupUI()
    }

    
    private func setupUI()
    {
        // 添加子控件
        contentView.addSubview(scrollview)
        scrollview.addSubview(iconView)
        
        // 布局子控件
        scrollview.frame = UIScreen.mainScreen().bounds
        
        // 处理缩放
        scrollview.delegate = self
        scrollview.maximumZoomScale = 2
        scrollview.minimumZoomScale = 0.5
        
        // 添加关闭手势
        let closeTap = UITapGestureRecognizer(target: self, action: #selector(PhotoBrowserCell.close))
        iconView.addGestureRecognizer(closeTap)
        iconView.userInteractionEnabled = true
    }
    
    /**
     关闭图片浏览器
     */
    func close()
    {
        photoBrowserCellDelegate?.photoBrowserCellDidClose(self)
    }
    
    // MARK: - 懒加载
    private lazy var scrollview = UIScrollView()
    
    lazy var iconView = UIImageView()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension PhotoBrowserCell: UIScrollViewDelegate
{
    // 需要缩放哪个空间
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
    {
        return iconView
    }
    
    // 缩放结束后调用此方法
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat)
    {
        var offsetX = (UIScreen.mainScreen().bounds.width - (view?.frame.width)!) * 0.5
        var offsetY = (UIScreen.mainScreen().bounds.height - (view?.frame.height)!) * 0.5
        
        offsetX = offsetX < 0 ? 0 : offsetX
        offsetY = offsetY < 0 ? 0 : offsetY
        scrollview.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: offsetY, right: offsetX)
    }
}